package com.gopay.common.util;

import java.util.HashMap;
import java.util.Map;

/**
 * 
 * @author kz-tu
 *
 */
public class StopWatch {
	
	private Map<String, Long> savepoints = new HashMap<String, Long>();
	
	public void start(String savepoint){
		savepoints.put(savepoint, System.currentTimeMillis());
	}
	
	/**
	 * Get elapsed time from savepoint and set save new savepoint
	 * @param getSp
	 * @param setSp
	 * @return
	 */
	public long getElapsedTime(String getSp){
		if(!savepoints.containsKey(getSp)){
			return -1;
		}
		
		return (System.currentTimeMillis() - savepoints.get(getSp));
	}
	
	/**
	 * Get elapsed time from savepoint and set new savepoint
	 * @param getSp
	 * @param setSp
	 * @return
	 */
	public long getElapsedTimeAndSet(String getSp, String setSp){
		if(setSp != null){
			savepoints.put(setSp, System.currentTimeMillis());
		}
		
		if(!savepoints.containsKey(getSp)){
			return -1;
		}
		
		return (System.currentTimeMillis() - savepoints.get(getSp));
	}
	
	public void stop(){
		savepoints.clear();
	}
}
